<template>
  <div class="user">
    <page-search
      :searchFormConfig="searchFormConfig"
      @searchBtnClick="hanldeSearchBtnClick"
      @resetBtnClick="hanldeResetBtnClick"
    ></page-search>
    <page-content
      ref="pageContentRef"
      :contentTableConfig="contentTableConfig"
      pageName="users"
      @newBtnClick="handleNewData"
      @handleEditorRow="handleEditorRow"
    ></page-content>
    <page-modal
      ref="pageModalRef"
      pageName="users"
      :modalConfig="modalConfigRef"
      :modalInfo="modalInfo"
    ></page-modal>
  </div>
</template>

<script lang="ts">
// import VueForm from "@/base-ui/form/index";
import { computed, defineComponent, PropType } from "vue";
import { useStore } from "@/store";
import pageModal from "@/components/page-modal";
import pageSearch from "@/components/page-search";
import pageContent from "@/components/page-content";

import { usePageSearch } from "@/hooks/usePageSearch";
import { usePageModal } from "@/hooks/usePageModal";

import { modalConfig } from "./config/modal.config";
import { searchFormConfig } from "./config/search.config";
import { contentTableConfig } from "./config/content.config";

// import PageModal from "@/components/page-modal/src/page-modal.vue";

export default defineComponent({
  components: {
    // VueForm,
    pageSearch,
    pageContent,
    pageModal,
  },
  setup() {
    const store = useStore();
    // 刷新数据
    const modalConfigRef = computed(() => {
      // 角色
      const roleOption = modalConfig.formItems.find(
        (item) => item.field === "roleId"
      );
      roleOption!.options = store.state.entireRoles.map((item) => {
        return { label: item.name, value: item.id };
      });

      // 部门
      const departmentOption = modalConfig.formItems.find(
        (item) => item.field === "departmentId"
      );
      departmentOption!.options = store.state.entireDepartments.map((item) => {
        return { label: item.name, value: item.id };
      });

      return modalConfig;
    });

    const { pageContentRef, hanldeSearchBtnClick, hanldeResetBtnClick } =
      usePageSearch();

    // 新增 回调函数
    const newCallBack = () => {
      const passwordItem = modalConfig.formItems.find(
        (item) => item.field === "password"
      );
      passwordItem!.isHidden = false;
    };
    // 编辑 回调函数
    const EditorCallBack = () => {
      const passwordItem = modalConfig.formItems.find(
        (item) => item.field === "password"
      );
      passwordItem!.isHidden = true;
    };

    const { modalInfo, pageModalRef, handleNewData, handleEditorRow } =
      usePageModal(newCallBack, EditorCallBack);

    return {
      pageContentRef,
      hanldeSearchBtnClick,
      hanldeResetBtnClick,
      searchFormConfig,
      contentTableConfig,
      modalConfigRef,
      pageModalRef,
      modalInfo,
      handleNewData,
      handleEditorRow,
    };
  },
});
</script>

<style lang="less" scoped></style>
